package com.wfg.flink.connector.mongodb.com;

import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.writer.ObjectWriter;

import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

/**
 * @author wfg
 */
public class MongoDateObjectWriter implements ObjectWriter<Date> {
    private static final SimpleDateFormat SIMPLE_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSXXX", Locale.US);
    @Override
    public void write(JSONWriter writer, Object object, Object o1, Type type, long l) {
        if (object == null) {
            writer.writeNull();
            return;
        }

        Date date = (Date) object;
        SIMPLE_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("UTC"));
        String dateStr = SIMPLE_DATE_FORMAT.format(date);
        writer.writeString(dateStr);
    }

}
